/*
 * @Author: szx
 * @Date: 2022-03-26 13:05:20
 * @LastEditTime: 2022-03-26 13:18:01
 * @Description:
 * @FilePath: \leetcode\剑指offer\49\49.js
 */
/**
 * @param {number} n
 * @return {number}
 */
var nthUglyNumber = function (n) {
    const dp = new Array(n).fill(0);
    dp[0] = 1;
    let p2 = 0,
        p3 = 0,
        p5 = 0;
    for (let i = 1; i < n; i++) {
        const num1 = dp[p2] * 2;
        const num2 = dp[p3] * 3;
        const num3 = dp[p5] * 5;

        dp[i] = Math.min(num1, Math.min(num2, num3));
        if (dp[i] == num1) p2++;
        if (dp[i] == num2) p3++;
        if (dp[i] == num3) p5++;
    }
    return dp[n - 1];
};
